<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>网点列表</span>
      </div>
    </template>
    <div style="text-align: right">
      <el-button type="primary" @click="add('add')">添加</el-button>
    </div>
    <el-table
      :data="tableData"
      class="mt20"
      :style="{ width: '100%' }"
      v-loading="loading"
    >
      <!-- <el-table-column prop="id" label="排序" /> -->
      <el-table-column label="排序" prop="sort">
        <template #default="scope">
          <el-popover
            v-model:visible="scope.row.sortVisible"
            placement="top"
            trigger="click"
            :width="160"
            :popper-style="{ width: '175px' }"
          >
            <el-input-number v-model="scope.row.sort" :min="1" />
            <div class="sort" @click="sort(scope.row)">确定</div>
            <template #reference>
              <span style="cursor: pointer">{{ scope.row.sort }}</span>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="网点" />
      <el-table-column label="地址" prop="status">
        <template #default="scope">
          <p>{{ scope.row.province + scope.row.city + scope.row.street }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="电话" />
      <el-table-column label="服务类型" prop="type">
        <template #default="scope">
          <p
            style="margin-right: 5px"
            v-for="(item, index) in scope.row.type"
            :key="index"
          >
            {{ code[item] }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <span>{{ scope.row.state == 1 ? '不通过' : '通过' }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作">
        <template #default="scope">
          <Edit @click="edit(scope.row, 'edit')" class="icon" />
          <Delete @click="detele(scope.row)" class="icon" />
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { serveceList, serveceDel } from '@/api/sericeNetwork/index';
import common from '@/modules';
const { sortListApi } = common;
const router = useRouter();
// loading
const loading = ref(false);
const tableConfig = ref({
  storeId: JSON.parse(localStorage.getItem('userDateInfo'))?.storeId,
  page: 1,
  pageSize: 100,
});
const tableData = ref([]);
const code = {
  1: '志愿填报',
  2: '心理咨询',
  3: '综合评估',
  4: '学前辅导',
  5: '家庭教育',
};

const sort = async (row) => {
  const res = await sortListApi({
    id: row.id,
    sort: row.sort,
    type: 'serviceSite',
  });
  // 隐藏排序弹出框
  row.sortVisible = false;
  ElMessage.success(res.msg);
  inti();
};

//新增按钮
const add = (name) => {
  router.push({ path: 'creat', query: { name } });
};
//编辑按钮
const edit = (item, name) => {
  router.push({ path: 'creat', query: { name, id: item.id } });
};
//删除按钮
const detele = (item) => {
  ElMessageBox.confirm('你确定要删除' + item.name + '吗?', '确定提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: false,
  })
    .then(() => {
      serveceDel({
        id: item.id,
        storeId: JSON.parse(localStorage.getItem('userDateInfo'))?.storeId,
      }).then((res) => {
        if (res.code === 20000) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          });
          inti();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete canceled',
      });
    });
};

const inti = () => {
  loading.value = true;
  serveceList(tableConfig.value).then((res) => {
    tableData.value = res.data.data;
    if (tableData.value.length) {
      tableData.value.forEach((item) => {
        item.sortVisible = false;
      });
    }
  });
  loading.value = false;
};

onMounted(() => {
  inti();
});
</script>
<style lang="scss" scoped>
@import '@/assets/styles/global.scss';
:deep(.el-table__inner-wrapper) {
  height: 500px;
}
:deep(.el-button--primary) {
  background-color: var(--primary-color) !important;
}
.sort {
  text-align: left;
  margin: 10px 0 0 0;
  cursor: pointer;
  color: #165dff;
}

.box-card {
  margin: 20px;

  .icon {
    width: 1.5em;
    height: 1.5em;
    margin-right: 8px;
    cursor: pointer;
  }
}
</style>
